package com.cz.ui.friend;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.R;
import com.cz.ui.chat.ChatInfo;
import com.cz.utils.DBOpenHelper;

import java.lang.reflect.Field;

public class FriendInfo extends AppCompatActivity {
    private DBOpenHelper dbOpenHelper;
    private ImageView head;
    private TextView remake;
    private TextView nickName;
    private TextView phone;
    private TextView address;
    private TextView postMessage;
    private String key;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_info);

        //获取需要渲染的组件
        head = this.findViewById(R.id.img_friends_head);
        remake = this.findViewById(R.id.tv_friends_name);
        nickName = this.findViewById(R.id.tv_friends_passName);
        phone = this.findViewById(R.id.tv_friends_phone);
        address = this.findViewById(R.id.tv_friends_address);
        postMessage = this.findViewById(R.id.tv_friends_post_message);
        showFriend();

        //点击发送消息事件
        postMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getBaseContext(), ChatInfo.class);
                intent.putExtra("nickName",key);
                startActivity(intent);
            }
        });

    }

    //好友信息申请并且渲染
    private void showFriend() {
        Intent intent = getIntent();
        String nickName = intent.getStringExtra("nickName");
        //好友信息申请并且渲染
        Log.i("需要查询的微信号是: ",nickName);

        dbOpenHelper = new DBOpenHelper(this,"WeChatMini",null,2);
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        //满足条件查询
        Cursor user = db.query("friends", null, "nickname=?",
                new String[]{nickName}, null, null, null);


        R.drawable drawable = new R.drawable();
        int resId=0;
        Field field= null;

        while(user.moveToNext()) {
            //根据查出来的数据封装成好友对象
            String uid = user.getString(0);
            String nickname = user.getString(1);
            key = nickname;
            String remake = user.getString(2);
            String phone = user.getString(3);
            String avatar = user.getString(4);
            String address = user.getString(5);
            Log.i("查询出的信息是: id:"+uid+",微信号:"+nickname+",备注:"+remake+",手机号:"+phone+",头像:"+avatar+",address:",address);


            this.remake.setText(remake);
            this.nickName.setText(this.nickName.getText()+nickName);
            this.phone.setText(this.phone.getText()+phone);
            this.address.setText(address);

            //头像渲染
            // 映射到drawable文件夹中,只需要提供图片名 可以省略后缀
            try {
                field = R.drawable.class.getField(avatar);
                resId=(Integer)field.get(drawable);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            this.head.setImageResource(resId);
        }
    }
    //用于解决intent传值问题
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
}
